#! /usr/bin/python
# -*- coding: cp1250 -*-
#credit to::http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/86554

import string, sys, os, re, time, urllib2, shutil
from os.path import *

yyyymmddThhmmss = lambda : re.sub('[-:]','', re.sub(' ','T', time.strftime ("%Y-%m-%d %H:%M:%S", time.localtime())))
#print yyyymmddThhmmss()

def isOnline(resource):
    """ check whethes is network available """
    #print isOnline(r'file:///G:/AFT/docs/afttw.html')
    #print isOnline(resource='http://www.google.com')
    try:
        response = urllib2.urlopen(resource)
    except:
        return False
    else:
        return True

def printPath(path, bytes, units='m'):
    if units == 'k':
        outputFile.write('%-8ldkB | %s | appx ' % (bytes / 1024, path))
        kBLeft_n = 4807105 - (bytes / 1024)
        outputFile.write(MBLeft_n+' kB to go')
    elif units == 'm':
        outputFile.write('%-5ldMB | %s | appx ' % (bytes / 1024 / 1024, path))
        MBLeft_n = 4691 - (bytes / 1024 / 1024)
        outputFile.write(str(MBLeft_n) +' MB to go')
    else:
        print '%-11ld|%s' % (bytes, path)

def dirSize(start, follow_links, my_depth, max_depth):

    total = 0L
    try:
        dir_list = os.listdir (start)
    except:
        if isdir (start):
            #print 'Cannot list directory %s' % start
            pass
        return 0
    for item in dir_list:
        path = '%s/%s' % (start, item)
        try:
            stats = os.stat (path)
        except:
            #print 'Cannot stat %s' % path
            continue
        total += stats[6]
        if isdir (path) and (follow_links or \
            (not follow_links and not islink (path))):
            bytes = dirSize (path, follow_links, my_depth + 1, max_depth)
            total += bytes
            if (my_depth < max_depth):
                printPath (path, bytes)
    return total

def checkG():
    outputFile.write('\n'+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime()))+' | ')
    #'Thu, 28 Jun 2001 14:17:15 +0000'

    paths = ['\\\\czprg0007\\Forensic']

    for path in paths:
        bytes = dirSize (path, follow_links=0, my_depth=0, max_depth=0)
        printPath (path, bytes, units='m')

if __name__ == "__main__":
    if isOnline(r'file:///G:/AFT/Forensic-Aft-ISACOPYCHANGESARELOST.html'):
        outputFile = open('aft-check-gdisk-size.tpl','a')
        checkG()
        outputFile.close()
        target_log_path = ''
        #G:/AFT/RPTS/aft-check-gdisk-size/'
        shutil.copy (target_log_path+'aft-check-gdisk-size.log',
                     target_log_path+'aft-check-gdisk-size.log.'+yyyymmddThhmmss())
        shutil.copy ('aft-check-gdisk-size.tpl', target_log_path+'aft-check-gdisk-size.log')
        os.system('start notepad.exe '+target_log_path+'aft-check-gdisk-size.log')
    else:
        time.sleep(2)
